﻿using Ldt.DataEngine.DataFlowParse.Model;
using Ldt.DataEngine.RuleAssemble;
using LdtDataEngin.ModuleDetail.BaseModule;
using LdtDataEngin.ModuleDetail.CommonModule.LawDeduce;
using LdtJudge.DataEngine.Common;
using LdtJudge.DataEngine.Common.CommHelp;
using LdtJudge.DataEngine.Common.StringAssemble;
using LdtJudge.DataEngine.Common.XmlUtility;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.DataModel.SystemtableModel;
using LdtJudge.DataEngine.Model.XMLConfigModel.CircumsatanModel;
using LdtJudge.DataEngine.Model.XMLConfigModel.DataStores;
using LdtJudge.DataEngine.Model.XMLConfigModel.LawElementModel;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml;

namespace LdtDataEngin.ModuleDetail.CommonModule.SituationPlotQuotLawExtr
{
    /// <summary>
    /// 情形情节法律法规的应用处理
    /// </summary>
    public class QutoLawHandle : AssermBase
    {
        #region 法律法规提取
        private CircumsatanModel circumsatanModel = new CircumsatanModel();
        /// <summary>
        /// 获取分析结果
        /// </summary>
        private DataSoursModel dataSoursModel = new DataSoursModel();
        private GetSysCaseCause DALSYS_CaseCause = new GetSysCaseCause();
        //提取法律法规
        private LawModelList LawModelList = new LawModelList();
        //合并法律法规
        private LawModelList LawModeMergelList = new LawModelList();
        private XmlUtilCommon xmlUtilCommon = new XmlUtilCommon();

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="context"></param>
        /// <param name="moduleName"></param>
        public QutoLawHandle(DataContext context, string moduleName)
        {
            base.dataContext = context;
            base.MoudleName = moduleName;
        }

        /// <summary>
        /// 法律法规的处理  这里只支持刑事法律法规的处理
        /// </summary>
        public void UsingLaw()
        {
            #region 配置文件解析
            //模块配置
            ConfigModel moudleConfig = dataContext.DataLayerSys.GetConfigModel(MoudleName);
            //配置编号
            string configCode = string.Empty;
            if (MoudleName.ToLower().IndexOf("ldtdataengin.moudle.moudlesituationplotquotlaw") > -1)
                configCode = "qxqjduclaw";
            else if (MoudleName.ToLower().IndexOf("ldtdataengin.moudle.moudlelawdeduce") > -1)
                configCode = "lawnodeperfect";
            //法律法规配置
            ConfigModel lawConfig = moudleConfig.ConfigDetail.FirstOrDefault(x => x.Code == configCode);
            string configContext = lawConfig?.ConfigContent;
            circumsatanModel = XmlUtility.DeserializeToObject<CircumsatanModel>(configContext);
            #endregion
            xmlUtilCommon.dataContext = dataContext;
            dataSoursModel.dataModelList = dataContext.DataLayerOpt.SourdatModleList;
            DocType docType = null;
            CasetypeModel casetypeM = circumsatanModel.casetypeModelList.FirstOrDefault(a => a.Code == dataContext.DataLayerAns.CaseType);
            if (casetypeM != null)
                docType = casetypeM.docTypeList.FirstOrDefault(a => a.Code.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower());
            if (docType != null)
            {
                //存在配置
                string lawmergpath = docType.lawmergepath;
                string laworgpath = circumsatanModel.casetypeModelList.FirstOrDefault(a => a.Code == dataContext.DataLayerAns.CaseType).laworgpath;

                //#region 提取法律规则
                //foreach (ParagraphModel paragraphcofig in docType.paragraphModelList)
                //{
                //    LawModelList = new LawModelList();
                //    foreach (PathModel pathModel in paragraphcofig.pathModelList)
                //    {
                //        //获取提取结果
                //        DataModel dataValueModel = XmlUtility.ConditionFirstOrDefaultDataModel(dataSoursModel.dataModelList, "", pathModel.value + "/详情");
                //        if (dataValueModel != null)
                //        {
                //            List<DataModel> dataValueList = XmlUtility.ConditionDataModelList(dataSoursModel.dataModelList, "", pathModel.value + "/详情");
                //            foreach (DataModel dataValue in dataValueList)
                //            {
                //                LawUsingModel lawUsingModel = new LawUsingModel();
                //                AttributeInfo attributeID = new AttributeInfo();
                //                AttributeInfo attributeSours = new AttributeInfo();
                //                //当前为情形节点
                //                lawUsingModel.Type = pathModel.Type;
                //                if (pathModel.Type == "1")
                //                {
                //                    attributeID = dataValue.AttributesList.FirstOrDefault(a => a.Name == "情形ID" && a.sourceList.FirstOrDefault(b => b.IsCheck == "1") != null);
                //                    attributeSours = dataValue.AttributesList.FirstOrDefault(a => a.Name == "情形来源" && a.sourceList.FirstOrDefault(b => b.IsCheck == "1") != null);
                //                }
                //                else if (pathModel.Type == "2")
                //                {
                //                    //当前为情节节点
                //                    attributeID = dataValue.AttributesList.FirstOrDefault(a => a.Name == "情节ID" && a.sourceList.FirstOrDefault(b=> b.IsCheck=="1")!=null);
                //                    attributeSours = dataValue.AttributesList.FirstOrDefault(a => a.Name == "情节来源" && a.sourceList.FirstOrDefault(b => b.IsCheck == "1") != null);
                //                }
                //                if (attributeID != null && attributeSours != null)
                //                {
                //                    lawUsingModel.CC_id = attributeID.sourceList.FirstOrDefault(a => a.IsCheck == "1").SaveValue;
                //                    lawUsingModel.Source = attributeSours.sourceList.FirstOrDefault(a => a.IsCheck == "1").SaveValue;
                //                    if ("legation,investigate,iefense".Split(',').Contains(lawUsingModel.Source.ToLower()))
                //                    {
                //                        #region legation处理方法
                //                        List<SYS_QuoteLawRel> dataTableLaw = DALSYS_CaseCause.GetSYS_QuoteLawRelByOLR_CaseCauseIDAndOLR_SituationID(dataContext.DataLayerAns.CaseCausesList[0].CC_ID, lawUsingModel.CC_id);
                //                        if (dataTableLaw.Count > 0)
                //                        {
                //                            foreach (SYS_QuoteLawRel dataRow in dataTableLaw)
                //                            {
                //                                string lawid = dataRow.OLR_LawID;
                //                                string lawname = dataRow.OLR_LawName;
                //                                LawModel lawmodel = AddLaw(lawid, lawname);
                //                                string provid = dataRow.OLR_Law_ProvID;
                //                                string provname = dataRow.OLR_Law_ProvName;
                //                                LawProv lawprov = AddLawPro(provid, provname, lawmodel);
                //                                string lawclauseid = dataRow.OLR_Law_ClauseNumber;
                //                                string lawclausename = dataRow.OLR_Law_ClauseName;
                //                                LawClause lawClause = AddLawClause(lawclauseid, lawclausename, lawprov);
                //                                string lawitemid = Convert.ToString(dataRow.OLR_Law_ItemNumber);
                //                                string lawitemname = Convert.ToString(dataRow.OLR_Law_ItemName);
                //                                if (!string.IsNullOrEmpty(lawitemid))
                //                                {
                //                                    AddLawItem(lawitemid, lawitemname, lawClause);
                //                                }
                //                            }
                //                        }
                //                        #endregion
                //                    }
                //                    else if (lawUsingModel.Source.ToLower() == "convicted")
                //                    {
                //                        #region convicted处理方法
                //                        //DataModel dataModelConvicted = dataSoursModel.dataModelList.FirstOrDefault(a => a.XmlPath == laworgpath);
                //                        DataModel dataModelConvicted = XmlUtility.ConditionFirstOrDefaultDataModel(dataSoursModel.dataModelList, "", laworgpath);
                //                        if (dataModelConvicted != null)
                //                        {
                //                            string orgText = dataModelConvicted.AttributesList.FirstOrDefault(a => a.Name == "原文").sourceList.FirstOrDefault(a => a.IsCheck == "1").SaveValue;
                //                            string oCollReg = RuleParseClass.GerRegValue("QuoteLawExtractA1");
                //                            MatchCollection oColl = Regex.Matches(orgText, oCollReg);
                //                            for (int i = 0; i < oColl.Count; i++)
                //                            {
                //                                Match oMatch = oColl[i];
                //                                string catname = oMatch.Groups[1].Value;
                //                                string newCatName = catname;
                //                                newCatName = Regex.Replace(catname, "<", "《");
                //                                newCatName = Regex.Replace(newCatName, ">", "》");
                //                                //查询获取相应的法规ID；
                //                                string LawLibraryCode = string.Empty;
                //                                //string CatID = QuoteLawUsing.QuoteLawExt.GetLawID(newCatName, out LawLibraryCode);
                //                                ///返回法律的数据对象
                //                                var CatIDmodel = QuoteLawExt.GetLawIDTupleModel(newCatName, out LawLibraryCode);
                //                                string quoteProvText = oMatch.Value;
                //                                if (CatIDmodel.Item1 == string.Empty)
                //                                {
                //                                    newCatName = Regex.Replace(catname, "<|>", "");
                //                                    CatIDmodel = QuoteLawExt.GetLawIDTupleModel(newCatName, out LawLibraryCode);
                //                                }
                //                                if (CatIDmodel.Item1 == string.Empty)
                //                                {
                //                                    newCatName = Regex.Replace(catname, "<|>", "%");
                //                                    CatIDmodel = QuoteLawExt.GetLawIDTupleModel(newCatName, out LawLibraryCode);
                //                                }
                //                                if (CatIDmodel.Item1 == string.Empty)
                //                                {
                //                                    continue;
                //                                }
                //                                string quoteLawID = string.Empty;
                //                                string quoteID = string.Empty;
                //                                ///法律的ID
                //                                string CatID = CatIDmodel.Item1;
                //                                LawModel lawModel = AddLaw(CatID, CatIDmodel.Item2);
                //                                string quoteProvItemCollReg = RuleParseClass.GerRegValue("QuoteLawExtractA3");
                //                                //匹配法律条款;
                //                                string tempReg = RuleParseClass.GerRegValue("QuoteLawExtractA4");
                //                                //将替换后的文本进行重新添加和拼接返回;正则操流程;
                //                                quoteProvText = RuleParseClass.GetParseValue("QuoteLawExtractA9", quoteProvText);
                //                                MatchCollection quoteProvItemColl = Regex.Matches(quoteProvText, quoteProvItemCollReg);
                //                                for (int j = 0; j < quoteProvItemColl.Count; j++)
                //                                {
                //                                    //循环添加引用发条
                //                                    Match provmatch = quoteProvItemColl[j];
                //                                    string singleQuoteprov = provmatch.Value;
                //                                    Match tempMatch = Regex.Match(singleQuoteprov, tempReg);
                //                                    string provName = (tempMatch == null) ? "" : tempMatch.Value;
                //                                    string provChineseNumb = (tempMatch == null) ? "" : tempMatch.Groups[1].Value;
                //                                    Match tempMatchtiao = Regex.Match(provName, "第.+?条之([一二三四五六七八九十零]+|[0-9]+)");
                //                                    int provNumbzhi = Convert.ToInt32(LdFormat.DecodeNumberCn(tempMatchtiao.Groups[1].Value));
                //                                    //条文序号
                //                                    int provNumb = Convert.ToInt32(LdFormat.DecodeNumberCn(provChineseNumb));
                //                                    //条文ID lijx 进行对该方法进行添加一个参数，表示之几
                //                                    string provID = QuoteLawExt.GetProvID(CatID, LawLibraryCode, provNumb, provNumbzhi);
                //                                    if (provChineseNumb.Trim().Equals(provNumb.ToString()))
                //                                    { //2018-03-12 ljx 这里的判断是用来表示如果法条内容是数字，需要进行转大写
                //                                        string daxie = LdFormat.CmycurD(provNumb);
                //                                        provName = Regex.Replace(provName, provNumb.ToString(), daxie);
                //                                    }
                //                                    if (provID == string.Empty || provChineseNumb == string.Empty)
                //                                    {
                //                                        continue;
                //                                    }
                //                                    string quoteProvID = string.Empty;
                //                                    LawProv lawProv = AddLawPro(provID, provName, lawModel);
                //                                    //添加款的项
                //                                    string MReg = RuleParseClass.GerRegValue("QuoteLawExtractA6");
                //                                    tempMatch = Regex.Match(singleQuoteprov, MReg);
                //                                    string clauseName = (tempMatch == null) ? "" : tempMatch.Value;
                //                                    string ClauseChineseNumb = (tempMatch == null) ? "" : tempMatch.Groups[1].Value;
                //                                    int ClauseNumb = Convert.ToInt32(LdFormat.DecodeNumberCn(ClauseChineseNumb));    //款序号
                //                                    if (ClauseChineseNumb.Trim().Equals(ClauseNumb.ToString()))
                //                                    { //2018-03-12 ljx 这里的判断是用来表示款内容是数字，需要进行转大写
                //                                        string daxie = LdFormat.CmycurD(ClauseNumb);
                //                                        clauseName = Regex.Replace(clauseName, ClauseNumb.ToString(), daxie);
                //                                    }
                //                                    LawClause lawClause = new LawClause();
                //                                    string quoteclauseID = string.Empty;
                //                                    if (ClauseChineseNumb != string.Empty)
                //                                    {
                //                                        //目前用的款ID，即款的序号。
                //                                        string clauseID = ClauseNumb.ToString();     // this.GetClauseID(CatID, provID, ClauseNumb, 0, true);                 //款ID
                //                                        if (clauseID != string.Empty)
                //                                        {
                //                                            lawClause = AddLawClause(clauseID, clauseName, lawProv);
                //                                        }
                //                                    }
                //                                    //添加项
                //                                    string TMReg = RuleParseClass.GerRegValue("QuoteLawExtractA8");
                //                                    tempMatch = Regex.Match(singleQuoteprov, TMReg);
                //                                    string ItemName = (tempMatch == null) ? "" : tempMatch.Value;
                //                                    string ItemChineseNumb = (tempMatch == null) ? "" : tempMatch.Groups[1].Value;
                //                                    int ItemNumb = Convert.ToInt32(LdFormat.DecodeNumberCn(ItemChineseNumb));        //项序号
                //                                    if (ItemChineseNumb.Trim().Equals(ItemNumb.ToString()))
                //                                    { //2018-03-12 ljx 这里的判断是用来表示项内容是数字，需要进行转大写
                //                                        string daxie = LdFormat.CmycurD(ItemNumb);
                //                                        ItemName = Regex.Replace(ItemName, ItemNumb.ToString(), daxie);
                //                                    }
                //                                    if (ItemChineseNumb != string.Empty)
                //                                    {
                //                                        //目前用的项ID，即项的序号。
                //                                        string itemID = ItemNumb.ToString();
                //                                        AddLawItem(itemID, ItemName, lawClause);
                //                                    }
                //                                }
                //                            }
                //                        }
                //                        #endregion
                //                    }
                //                }
                //            }

                //        }
                //    }
                //    //计算出法律数据进行转换XML存储
                //    if (LawModelList.lawModels.Count > 0)
                //    {
                //        string xmllawstr = AnalysisModelLaw(LawModelList);
                //        XmlDocument xmlDocumentlaw = new XmlDocument();
                //        xmlDocumentlaw.LoadXml(xmllawstr);
                //        XmlNodeList xmlNodeList = xmlDocumentlaw.SelectNodes("引用法规/*");
                //        foreach (XmlNode xmlNode in xmlNodeList)
                //        {
                //            XmlDocument xmlDocumentlawlist = new XmlDocument();
                //            xmlDocumentlawlist.LoadXml(xmlNode.OuterXml);
                //            DataSoursModel dataSours = xmlUtilCommon.GetXMLByDataModel(xmlDocumentlawlist);
                //            DataModel dataModelprent = XmlUtility.ConditionFirstOrDefaultDataModel(dataSoursModel.dataModelList, "", paragraphcofig.lawpath);
                //            if (dataModelprent == null)
                //            {
                //                xmlUtilCommon.CaseNodeUpdate(dataSoursModel.dataModelList, paragraphcofig.lawpath);
                //                dataModelprent = XmlUtility.ConditionFirstOrDefaultDataModel(dataSoursModel.dataModelList, "", paragraphcofig.lawpath);
                //            }
                //            foreach (DataModel modeldata in dataSours.dataModelList)
                //            {
                //                modeldata.XmlPath = paragraphcofig.lawpath + "/" + modeldata.XmlPath;
                //            }
                //            List<DataModel> dataModelListLaw = dataSours.dataModelList;
                //            MergeLaw(LawModelList, LawModeMergelList);
                //            XmlNode xmlNodeRoot= dataContext.DataLayerOpt.OptimizationXml.SelectSingleNode(lawmergpath);
                            
                //        }
                //    }
                //}
                //#endregion

                //#region 合并法律规则
                //if (LawModeMergelList.lawModels.Count > 0 && !string.IsNullOrEmpty(lawmergpath))
                //{

                //    LawModelList modelLaw = new LawModelList();
                //    modelLaw = LawModeMergelList;
                //    string xmllawstr = AnalysisModelLaw(modelLaw);
                //    XmlDocument xmlDocumentlaw = new XmlDocument();
                //    xmlDocumentlaw.LoadXml(xmllawstr);
                //    XmlNodeList xmlNodeList = xmlDocumentlaw.SelectNodes("引用法规/*");
                //    foreach (XmlNode xmlNode in xmlNodeList)
                //    {
                //        XmlDocument xmlDocumentlawlist = new XmlDocument();
                //        xmlDocumentlawlist.LoadXml(xmlNode.OuterXml);
                //        DataSoursModel dataSours = xmlUtilCommon.GetXMLByDataModel(xmlDocumentlawlist);
                //        DataModel dataModelprent = XmlUtility.ConditionFirstOrDefaultDataModel(dataSoursModel.dataModelList, "", lawmergpath);
                //        if (dataModelprent == null)
                //        {
                //            xmlUtilCommon.CaseNodeUpdate(dataSoursModel.dataModelList, lawmergpath);
                //            if (lawmergpath.IndexOf("[last()]") > -1)
                //                lawmergpath = lawmergpath.Replace("[last()]", "");
                //            dataModelprent = dataSoursModel.dataModelList.FirstOrDefault(a => a.XmlPath == lawmergpath);
                //        }
                //        foreach (DataModel modeldata in dataSours.dataModelList)
                //        {
                //            modeldata.XmlPath = lawmergpath + "/" + modeldata.XmlPath;
                //            modeldata.XmlPath = modeldata.XmlPath.Replace(@"/详情/详情", "/详情");
                //        }
                //        List<DataModel> dataModelListLaw = dataSours.dataModelList;
                //        dataModelListLaw.FirstOrDefault(a => a.ParentId == "").ParentId = dataModelprent.ID;

                //        //dataSoursModel.dataModelList.AddRange(dataModelListLaw);
                //        lawDataModelAdd(dataSoursModel.dataModelList, dataModelListLaw, dataModelprent.ID);
                //        XmlDocument xmlDocumentop = XmlUtility.DataModelByXml(dataSoursModel.dataModelList);
                //    }


                //}
                //#endregion
            }
            dataContext.DataLayerOpt.SourdatModleList = dataSoursModel.dataModelList;
            dataContext.DataLayerOpt.OptimizationXml = XmlUtility.DataModelByXml(dataContext.DataLayerOpt.SourdatModleList);
        }

        private void lawDataModelAdd(List<DataModel> dataModelList, List<DataModel> dataModeLLawList,string parid) 
        {
            var list = dataModeLLawList.Where(a => a.ParentId == parid);
            if (list!=null)
            {
                foreach (DataModel law in list)
                {
                    if (law.AttributesList != null)
                    {
                        if (law.AttributesList.FirstOrDefault(a => a.Name.ToLower() == "引用法规id") != null)
                        {
                            string value = law.AttributesList.FirstOrDefault(a => a.Name.ToLower() == "引用法规id").sourceList[0].SaveValue;
                            string xmlpath = law.XmlPath + @"[@引用法规ID='" + value + "']";
                            XmlNode xmlNode = dataContext.DataLayerOpt.OptimizationXml.SelectSingleNode(xmlpath);
                            if (xmlNode != null)
                            {
                                lawDataModelAdd(dataModelList, dataModeLLawList, law.ID);
                                continue;
                            }
                        }
                        else if (law.AttributesList.FirstOrDefault(a => a.Name.ToLower() == "引用条id") != null)
                        {
                            string value = law.AttributesList.FirstOrDefault(a => a.Name.ToLower() == "引用条id").sourceList[0].SaveValue;
                            string xmlpath = law.XmlPath + @"[@引用条ID='" + value + "']";
                            XmlNode xmlNode = dataContext.DataLayerOpt.OptimizationXml.SelectSingleNode(xmlpath);
                            if (xmlNode != null)
                            {
                                lawDataModelAdd(dataModelList, dataModeLLawList, law.ID);
                                continue;
                            }
                        }
                        else if (law.AttributesList.FirstOrDefault(a => a.Name.ToLower() == "款排序") != null)
                        {
                            string value = law.AttributesList.FirstOrDefault(a => a.Name.ToLower() == "款排序").sourceList[0].SaveValue;
                            string xmlpath = law.XmlPath + @"[@款排序='" + value + "']";
                            XmlNode xmlNode = dataContext.DataLayerOpt.OptimizationXml.SelectSingleNode(xmlpath);
                            if (xmlNode != null)
                            {
                                lawDataModelAdd(dataModelList, dataModeLLawList, law.ID);
                                continue;
                            }
                        }
                        else if (law.AttributesList.FirstOrDefault(a => a.Name.ToLower() == "项排序") != null)
                        {
                            string value = law.AttributesList.FirstOrDefault(a => a.Name.ToLower() == "项排序").sourceList[0].SaveValue;
                            string xmlpath = law.XmlPath + @"[@项排序='" + value + "']";
                            XmlNode xmlNode = dataContext.DataLayerOpt.OptimizationXml.SelectSingleNode(xmlpath);
                            if (xmlNode != null)
                            {
                                lawDataModelAdd(dataModelList, dataModeLLawList, law.ID);
                                continue;
                            }
                        }
                    }

                    dataModelList.Add(law);
                    lawDataModelAdd(dataModelList, dataModeLLawList, law.ID);
                }
            }
           
        }

        /// <summary>
        /// 进行法律法规合并操作
        /// </summary>
        /// <param name="modelLaw"></param>
        /// <param name="resultLaw"></param>
        /// <returns></returns>
        private void MergeLaw(LawModelList modelLaw, LawModelList resultLaw) 
        {
            foreach (var law in modelLaw.lawModels)
            {
                var lawresult = resultLaw.lawModels.FirstOrDefault(a => a.LawID == law.LawID);
                if (lawresult==null)
                {
                    resultLaw.lawModels.Add(law);
                }
                else
                {
                    foreach (var Prov in law.lawProvList)
                    {
                        var provresult = lawresult.lawProvList.FirstOrDefault(a => a.ProvID == Prov.ProvID);
                        if (provresult==null)
                        {
                            lawresult.lawProvList.Add(Prov);
                        }
                        else
                        {
                            foreach (var clause in Prov.lawClauseList)
                            {
                                var clauseresult = provresult.lawClauseList.FirstOrDefault(a => a.ClauseID == clause.ClauseID);
                                if (clauseresult==null)
                                {
                                    provresult.lawClauseList.Add(clause);
                                }
                                else
                                {
                                    foreach (var item in clause.lawItemList)
                                    {
                                        var itemresult = clauseresult.lawItemList.FirstOrDefault(a => a.ItemID == item.ItemID);

                                        if (itemresult==null)
                                        {
                                            clauseresult.lawItemList.Add(item);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        
        }



        /// <summary>
        /// 解析法律实体为XML结构
        /// </summary>
        /// <param name="modelLaw"></param>
        /// <returns></returns>
        public string AnalysisModelLaw(LawModelList modelLaw)
        {
            XmlDocument xmlDocument = new XmlDocument();
            XmlNode xmlNodeRoot = xmlDocument.CreateElement("引用法规");
            if (modelLaw.lawModels.Count > 0)
            {
                //XmlNode xmlNodeLaw = xmlDocument.CreateElement("引用法规");
                foreach (LawModel lawModel in modelLaw.lawModels)
                {
                    XmlNode xmlNodeLawModle = xmlDocument.CreateElement("详情");
                    if (!string.IsNullOrEmpty(lawModel.LawID))
                    {
                        XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("引用法规ID");
                        xmlAttribute.Value = lawModel.LawID;
                        xmlNodeLawModle.Attributes.Append(xmlAttribute);
                    }
                    if (!string.IsNullOrEmpty(lawModel.LawName))
                    {
                        XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("引用法规名称");
                        xmlAttribute.Value = lawModel.LawName;
                        xmlNodeLawModle.Attributes.Append(xmlAttribute);

                        XmlAttribute xmlAttributes = xmlDocument.CreateAttribute("法规来源");
                        xmlAttributes.Value = "legation";
                        xmlNodeLawModle.Attributes.Append(xmlAttributes);
                    }
                    if (lawModel.lawProvList.Count > 0)
                    {
                        XmlNode xmlNodeProv = xmlDocument.CreateElement("引用条");
                        foreach (LawProv lawProv in lawModel.lawProvList)
                        {
                            XmlNode xmlNodeProvcontxt = xmlDocument.CreateElement("详情");
                            if (!string.IsNullOrEmpty(lawProv.ProvID))
                            {
                                XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("引用条ID");
                                xmlAttribute.Value = lawProv.ProvID;
                                xmlNodeProvcontxt.Attributes.Append(xmlAttribute);
                            }
                            if (!string.IsNullOrEmpty(lawProv.ProvName))
                            {
                                XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("引用条名称");
                                xmlAttribute.Value = lawProv.ProvName;
                                xmlNodeProvcontxt.Attributes.Append(xmlAttribute);
                            }
                            if (!string.IsNullOrEmpty(lawProv.IsConviction))
                            {
                                XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("是否定罪法条");
                                xmlAttribute.Value = lawProv.IsConviction;
                                xmlNodeProvcontxt.Attributes.Append(xmlAttribute);
                            }
                            if (lawProv.lawClauseList.Count > 0)
                            {
                                XmlNode xmlNodeClause = xmlDocument.CreateElement("引用款");
                                foreach (LawClause lawClause in lawProv.lawClauseList)
                                {
                                    XmlNode xmlNodeClauseTxt = xmlDocument.CreateElement("详情");
                                    if (!string.IsNullOrEmpty(lawClause.ClauseID))
                                    {
                                        XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("款排序");
                                        xmlAttribute.Value = lawClause.ClauseID;
                                        xmlNodeClauseTxt.Attributes.Append(xmlAttribute);
                                    }
                                    if (!string.IsNullOrEmpty(lawClause.ClauseName))
                                    {
                                        XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("引用款名称");
                                        xmlAttribute.Value = lawClause.ClauseName;
                                        xmlNodeClauseTxt.Attributes.Append(xmlAttribute);
                                    }
                                    if (lawClause.lawItemList.Count > 0)
                                    {
                                        XmlNode xmlNodeItem = xmlDocument.CreateElement("引用项");
                                        foreach (LawItem lawItem in lawClause.lawItemList)
                                        {
                                            XmlNode xmlNodeItemTxt = xmlDocument.CreateElement("详情");
                                            if (!string.IsNullOrEmpty(lawItem.ItemID))
                                            {
                                                XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("项排序");
                                                xmlAttribute.Value = lawItem.ItemID;
                                                xmlNodeItemTxt.Attributes.Append(xmlAttribute);
                                            }
                                            if (!string.IsNullOrEmpty(lawItem.ItemName))
                                            {
                                                XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("引用项名称");
                                                xmlAttribute.Value = lawItem.ItemName;
                                                xmlNodeItemTxt.Attributes.Append(xmlAttribute);
                                            }
                                            xmlNodeItem.AppendChild(xmlNodeItemTxt);
                                        }
                                        xmlNodeClauseTxt.AppendChild(xmlNodeItem);
                                        ;
                                    }
                                    xmlNodeClause.AppendChild(xmlNodeClauseTxt);
                                }
                                xmlNodeProvcontxt.AppendChild(xmlNodeClause);
                            }
                            xmlNodeProv.AppendChild(xmlNodeProvcontxt);
                        }
                        xmlNodeLawModle.AppendChild(xmlNodeProv);
                    }
                    xmlNodeRoot.AppendChild(xmlNodeLawModle);
                }
                //xmlNodeRoot.AppendChild(xmlNodeLaw);
                xmlDocument.AppendChild(xmlNodeRoot);
            }
            return xmlDocument.OuterXml;
        }


        
        /// <summary>
        /// 添加法律实体
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public LawModel AddLaw(string id, string name)
        {
            LawModel lawModelLaw = LawModelList.lawModels.FirstOrDefault(a => a.LawID == id);
            if (lawModelLaw == null && !string.IsNullOrEmpty(id))
            {
                LawModel lawModel = new LawModel();
                lawModel.LawID = id;
                lawModel.LawName = name;
                lawModelLaw = lawModel;
                LawModelList.lawModels.Add(lawModelLaw);
            }
            return lawModelLaw;
        }

        /// <summary>
        /// 添加法条
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="lawModelLaw"></param>
        /// <returns></returns>
        public LawProv AddLawPro(string id, string name, LawModel lawModelLaw)
        {
            LawProv lawProvLaw = lawModelLaw.lawProvList.FirstOrDefault(a => a.ProvID == id);
            if (lawProvLaw == null && !string.IsNullOrEmpty(id))
            {
                LawProv lawProv = new LawProv();
                lawProv.ProvID = id;
                lawProv.ProvName = name;
                Dictionary<string, string> dic = QuoteLawExt.GetLawprovModelByID(id);
                if (dic.ContainsKey("IsConviction"))
                {
                    lawProv.IsConviction = dic["IsConviction"];
                    if (lawProv.IsConviction == "true")
                    {
                        lawProv.IsConviction = "1";
                    }
                    else
                    {
                        lawProv.IsConviction = "0";
                    }
                }
                lawModelLaw.lawProvList.Add(lawProv);
                lawProvLaw = lawProv;

            }
            return lawProvLaw;
        }

        /// <summary>
        /// 法款添加
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="lawProvLaw"></param>
        /// <returns></returns>
        public LawClause AddLawClause(string id, string name, LawProv lawProvLaw)
        {
            LawClause lawClauseLaw = lawProvLaw.lawClauseList.FirstOrDefault(a => a.ClauseID == id);
            if (lawClauseLaw == null && !string.IsNullOrEmpty(id))
            {
                LawClause lawClause = new LawClause();
                lawClause.ClauseID = id;
                lawClause.ClauseName = name;
                lawProvLaw.lawClauseList.Add(lawClause);
                lawClauseLaw = lawClause;
            }
            return lawClauseLaw;
        }

        /// <summary>
        /// 法项添加
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="lawClauseLaw"></param>
        /// <returns></returns>
        public LawItem AddLawItem(string id, string name, LawClause lawClauseLaw)
        {
            if (lawClauseLaw == null)
            {
                return null;
            }
            LawItem lawItemLaw = lawClauseLaw.lawItemList.FirstOrDefault(a => a.ItemID == id);
            if (lawItemLaw == null && !string.IsNullOrEmpty(id))
            {
                LawItem lawItem = new LawItem();
                lawItem.ItemID = id;
                lawItem.ItemName = name;
                lawClauseLaw.lawItemList.Add(lawItem);
                lawItemLaw = lawItem;
            }
            return lawItemLaw;
        }
        #endregion

    }
    /// <summary>
    /// 法律推导
    /// </summary>
    public class LawUsingModel
    {
        /// <summary>
        ///公诉/当庭宣判
        /// </summary>
        public string Source;
        /// <summary>
        /// 来源id
        /// </summary>
        public string CC_id;
        /// <summary>
        /// 1情节2情形
        /// </summary>
        public string Type;
    }
}
